package com.xx.doublexchat.common.user.adapter;

import com.xx.doublexchat.common.user.domain.entity.FriendApplyResponse;
import com.xx.doublexchat.common.user.domain.entity.User;
import com.xx.doublexchat.common.user.domain.entity.UserApply;
import com.xx.doublexchat.common.user.domain.entity.UserFriend;
import com.xx.doublexchat.common.user.domain.enums.ApplyReadStatusEnum;
import com.xx.doublexchat.common.user.domain.enums.ApplyStatusEnum;
import com.xx.doublexchat.common.user.domain.enums.ApplyTypeEnum;
import com.xx.doublexchat.common.user.domain.vo.request.FriendApplyRequest;
import com.xx.doublexchat.common.user.domain.vo.response.FriendResponse;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class FriendAdapter {
    /**
     * 封装好友信息
     *
     * @param list
     * @param userList
     * @return
     */
    public static List<FriendResponse> buildFriend(List<UserFriend> list, List<User> userList) {
        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));
        return list.stream().map(userFriend -> {
            FriendResponse resp = new FriendResponse();
            resp.setUid(userFriend.getFriendUid());
            User user = userMap.get(userFriend.getFriendUid());
            if (Objects.nonNull(user)) {
                resp.setActiveStatus(user.getActiveStatus());
            }
            return resp;
        }).collect(Collectors.toList());
    }

    /**
     * 封装申请信息
     *
     * @param uid
     * @param request
     * @return
     */
    public static UserApply buildFriendApply(Long uid, FriendApplyRequest request) {
        UserApply userApplyNew = new UserApply();
        userApplyNew.setUid(uid);
        userApplyNew.setMsg(request.getMsg());
        userApplyNew.setType(ApplyTypeEnum.ADD_FRIEND.getCode());
        userApplyNew.setTargetId(request.getTargetUid());
        userApplyNew.setStatus(ApplyStatusEnum.WAIT_APPROVAL.getCode());
        userApplyNew.setReadStatus(ApplyReadStatusEnum.UNREAD.getCode());
        return userApplyNew;
    }

    /**
     * 封装添加好友列表信息
     * @param records
     * @return
     */
    public static List<FriendApplyResponse> buildFriendApplyList(List<UserApply> records) {
        return records.stream().map(userApply -> {
            FriendApplyResponse friendApplyResp = new FriendApplyResponse();
            friendApplyResp.setUid(userApply.getUid());
            friendApplyResp.setType(userApply.getType());
            friendApplyResp.setApplyId(userApply.getId());
            friendApplyResp.setMsg(userApply.getMsg());
            friendApplyResp.setStatus(userApply.getStatus());
            return friendApplyResp;
        }).collect(Collectors.toList());
    }
}
